package com.example.bloodbankapplication.fragment;

import java.util.List;

import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.bloodbankapplication.R;
import com.example.bloodbankapplication.location.MyLocation;

public class DonorDetailsFragment extends BaseFragment{

	private TextView tvName,tvPhno,tvBloodgroup,tvState,tvCity,tvMap,tvRoute;
	private String state,city;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.donor_details, null);
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		tvName = (TextView) getView().findViewById(R.id.name);
		tvPhno = (TextView) getView().findViewById(R.id.phno);
		tvBloodgroup = (TextView) getView().findViewById(R.id.bloodgroup);
		tvState = (TextView) getView().findViewById(R.id.state);
		tvCity = (TextView) getView().findViewById(R.id.city);
		tvMap = (TextView) getView().findViewById(R.id.map);
		tvRoute = (TextView) getView().findViewById(R.id.route);
		
		Bundle bundle =  getArguments();
		tvName.setText(bundle.getString("Name"));
		tvPhno.setText(bundle.getString("PHNO"));
		tvBloodgroup.setText(bundle.getString("BG"));
		tvState.setText(bundle.getString("State"));
		tvCity.setText(bundle.getString("City"));
		
		state = bundle.getString("State");
		city = bundle.getString("City");
		 
		tvMap.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				getLocationFromAddress(state+","+city);
			}
		});
		
		tvRoute.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
			
				MyLocation loc = new MyLocation(getActivity(),state+","+city);
				loc.getLocations();

			}
		});
		
	}
	
	
	private  void getLocationFromAddress(String strAddress){

		Geocoder coder = new Geocoder(getActivity());
		List<Address> address;

		try{
			address = coder.getFromLocationName(strAddress,5);
			Address location = address.get(0);
			
			
			String latlong = String.valueOf(location.getLatitude())+","+String.valueOf(location.getLongitude());
			 Intent intent = new Intent(Intent.ACTION_VIEW);
	         intent.setData(Uri.parse("geo:0,0?q=" + (latlong)));
	         try {
	             startActivity(intent);
	         } catch (Exception e) {
	        	 Log.i("NCS","Exception :"+e.getMessage());
	         }
			
		}
		catch(Exception ex){

		}



	}
}
